package gv.trans.dict;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class PHPConnector {
	
	public static String execute(String cmd){

		try {
			URL url = new URL("http://www.giuseppevalerio.com/namestranslator/dictionary.php?cmd="+cmd);
			//URL url = new URL("http://www.google.it");
			HttpURLConnection urlConnection = (HttpURLConnection) url
					.openConnection();
			InputStream in = new BufferedInputStream(
					urlConnection.getInputStream());
			readStream(in);
			urlConnection.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//System.out.println(page);
		return page;
	}
	
	static String page = "";
	private static void readStream(InputStream in) {
		page = "";
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		try {
			int i=0;
			char[] buf = new char[1];
			while(i!=-1){
				//i++;
				try{
					i = br.read(buf);
					if(i!=-1)
					page=page+new String(buf);
				}catch(NullPointerException npe){
					break;
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
